Deep CNN Models

Constructing and training your own ConvNet from scratch can be Hard and a long task.

A common trick used in Deep Learning is to use a pre-trained model and finetune it to the specific data it will be used for.

Famous Models with Keras

This notebook contains code and reference for the following Keras models (gathered from https://github.com/fchollet/keras/tree/master/keras/applications)

  • VGG16
  • VGG19
  • ResNet50
  • Inception v3
  • Xception
  • ... more to come

References

All architectures are compatible with both TensorFlow and Theano, and upon instantiation the models will be built according to the image dimension ordering set in your Keras configuration file at ~/.keras/keras.json.

For instance, if you have set image_data_format="channels_last", then any model loaded from this repository will get built according to the TensorFlow dimension ordering convention, "Width-Height-Depth".

VGG16

VGG19

keras.applications


In [1]:
from keras.applications import VGG16
from keras.applications.imagenet_utils import preprocess_input, decode_predictions
import os


Using TensorFlow backend.

In [2]:
# -- Jupyter/IPython way to see documentation
# please focus on parameters (e.g. include top)
VGG16??

In [3]:
vgg16 = VGG16(include_top=True, weights='imagenet')


Downloading data from https://github.com/fchollet/deep-learning-models/releases/download/v0.1/vgg16_weights_tf_dim_ordering_tf_kernels.h5

If you're wondering where this HDF5 files with weights is stored, please take a look at ~/.keras/models/

HandsOn VGG16 - Pre-trained Weights


In [6]:
IMAGENET_FOLDER = 'imgs/imagenet'  #in the repo

In [10]:
!ls imgs/imagenet


apricot_565.jpeg     apricot_787.jpeg     strawberry_1174.jpeg
apricot_696.jpeg     strawberry_1157.jpeg strawberry_1189.jpeg


In [13]:
from keras.preprocessing import image
import numpy as np

img_path = os.path.join(IMAGENET_FOLDER, 'strawberry_1157.jpeg')
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print('Input image shape:', x.shape)

preds = vgg16.predict(x)
print('Predicted:', decode_predictions(preds))


Input image shape: (1, 224, 224, 3)
Downloading data from https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json
Predicted: [[('n07745940', 'strawberry', 0.98483676), ('n07836838', 'chocolate_sauce', 0.0073711565), ('n07614500', 'ice_cream', 0.0030998574), ('n04332243', 'strainer', 0.0025101686), ('n04476259', 'tray', 0.00060175249)]]


In [14]:
img_path = os.path.join(IMAGENET_FOLDER, 'apricot_696.jpeg')
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print('Input image shape:', x.shape)

preds = vgg16.predict(x)
print('Predicted:', decode_predictions(preds))


Input image shape: (1, 224, 224, 3)
Predicted: [[('n07747607', 'orange', 0.87526792), ('n07749582', 'lemon', 0.03620464), ('n07717556', 'butternut_squash', 0.021843448), ('n03937543', 'pill_bottle', 0.0126132), ('n03942813', 'ping-pong_ball', 0.0054204506)]]


In [15]:
img_path = os.path.join(IMAGENET_FOLDER, 'apricot_565.jpeg')
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
print('Input image shape:', x.shape)

preds = vgg16.predict(x)
print('Predicted:', decode_predictions(preds))


Input image shape: (1, 224, 224, 3)
Predicted: [[('n07718472', 'cucumber', 0.29338178), ('n07716358', 'zucchini', 0.2383192), ('n04596742', 'wok', 0.042132568), ('n07716906', 'spaghetti_squash', 0.038422), ('n07711569', 'mashed_potato', 0.036552209)]]

Hands On:

Try to do the same with VGG19 Model


In [ ]:
# from keras.applications import VGG19

Residual Networks

ResNet 50


In [ ]:
## from keras.applications import ...

In [ ]: